package game.manager
{
	import flash.events.Event;
	import flash.events.EventDispatcher;
	import flash.net.URLLoader;
	import flash.net.URLRequest;
	
	/**
	 * @author Kirill Pletnev
	 */
	public class ConfigurationManager extends EventDispatcher
	{
		/**
		 * путь к текстурам
		 */
		private var _resPath:String = "../media/textures/";
		/**
		 * путь к текстурам тайов
		 */
		private var _pathToTileRes:String;
		/**
		 * версия движка
		 */
		private var _version:String;
		/**
		 * ширина тайла
		 * значение должно быть одним из: 2, 4, 8, 16, 32, 64, 128, 256, 512, 1024, 2048
		 */
		private var _tileWidth:int;
		/**
		 * высота тайла должна равняться половине ширины
		 */
		private var _tileHeight:int = _tileWidth >> 1;
		private var _tileHalfWidth:int = _tileWidth >> 1;
		private var _tileHalfHeight:int = _tileHeight >> 1;
		/**
		 * true - режим дебага
		 */
		private var _isDebugMode:Boolean;
		/**
		 * true - включает режимы редактора карт и контент-менеджера
		 */
		private var _isEditor:Boolean;
		private var _tileDataUrl:String;
		private var _mapDataUrl:String;
		private var _mapCursorColor:int;
		private var _mapCursorAlpha:Number;
		
		////////////////////////////////////////////////////////////////////
		//	load configuration xml	
		////////////////////////////////////////////////////////////////////
		/*
		 * нужно передать во флеш варсах
		 */
		public static const CONFIG_URL:String = "../data/config.xml";
		private static const _instance:ConfigurationManager = new ConfigurationManager();
		private var _loaded:Boolean = false;
		
		public function ConfigurationManager()
		{
			if (_instance != null)
			{
				throw new Error("ConfigurationManager instance already existing in Singleton");
			}
		}
		
		static public function get instance():ConfigurationManager
		{
			return _instance;
		}
		
		public function loadAll(fileUrl:String):void
		{
			var loader:URLLoader = new URLLoader();
			var request:URLRequest = new URLRequest(fileUrl);
			loader.addEventListener(Event.COMPLETE, onXmlDataLoaded);
			loader.load(request);
		}
		
		public function onXmlDataLoaded(e:Event):void
		{
			var data:XML = new XML(e.currentTarget.data);
			
			_tileWidth = parseInt(data.TILE_WIDTH);
			_tileHeight = _tileWidth >> 1;
			_tileHalfWidth = _tileWidth >> 1;
			_tileHalfHeight = _tileHeight >> 1;
			
			_tileDataUrl = data.TILES_DATA_URL;
			_mapDataUrl = data.MAP_DATA_URL;
			
			_resPath = data.RES_PATH;
			_pathToTileRes = data.PATH_TO_TILE_RES;
			
			_mapCursorColor = parseInt(data.MAP_CURSOR_COLOR);
			_mapCursorAlpha = parseFloat(data.MAP_CURSOR_ALPHA);
			
			_isDebugMode = data.IS_DEBUG_MODE == "true";
			_isEditor = data.IS_EDITOR == "true";
			_version = data.VERSION;
			
			finishLoading();
		}
		
		private function finishLoading():void
		{
			_loaded = true;
			dispatchEvent(new Event(Event.COMPLETE));
		}
		
		// GETTERS & SETTERS =====================================================================
		public function get loaded():Boolean
		{
			return _loaded;
		}
		
		public function get resPath():String
		{
			return _resPath;
		}
		
		public function get pathToTileRes():String
		{
			return _pathToTileRes;
		}
		
		public function get version():String
		{
			return _version;
		}
		
		public function get mapCursorAlpha():Number
		{
			return _mapCursorAlpha;
		}
		
		public function get mapCursorColor():int
		{
			return _mapCursorColor;
		}
		
		public function get mapDataUrl():String
		{
			return _mapDataUrl;
		}
		
		public function get tileDataUrl():String
		{
			return _tileDataUrl;
		}
		
		public function get isEditor():Boolean
		{
			return _isEditor;
		}
		
		public function get isDebugMode():Boolean
		{
			return _isDebugMode;
		}
		
		public function get tileHalfHeight():int
		{
			return _tileHalfHeight;
		}
		
		public function get tileHalfWidth():int
		{
			return _tileHalfWidth;
		}
		
		public function get tileHeight():int
		{
			return _tileHeight;
		}
		
		public function get tileWidth():int
		{
			return _tileWidth;
		}
	}
}